package com.hexorld.core;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.RadialGradient;
import android.graphics.Shader.TileMode;

import com.hexorld.utils.Position;

public class DColorBar extends BasicBar {
	private int mBeginColor = 0;
	private int mEndColor = 0;
	
	public DColorBar(Context context, int width, int height, int maxValue,
			Position p) {
		super(context, width, height, maxValue, p);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
	
	}
	
	/**
	 * set begin&end color for bar
	 * @param colorBegin
	 * @param colorEnd
	 */
	public void setColor(int colorBegin, int colorEnd){
		mBeginColor = colorBegin;
		mEndColor = colorEnd;
	}
	
	/**
	 * Get current color by BeginColor and EndColor Value
	 * @return
	 */
	@Override
	protected void getBarColor(){
		int dis = mEndColor - mBeginColor;
		
		mBarColor = mBeginColor + (dis * mCurrentValue / mMaxValue);
		setShader();
	}
	
	private void setShader(){
		int colors[] = {0xffff0000, 0xff0000ff, 0xff00ff00}; 
		float positions[] = {0f, 0.5f, 1.0f}; //key points for each color!S
		LinearGradient lShader = new LinearGradient(mDstRect.left, mDstRect.top + mDstRect.height()/2, mDstRect.right, mDstRect.top + mDstRect.height()/2, colors, positions, TileMode.CLAMP);//new LinearGradient(mDstRect.left, mDstRect.top + mDstRect.height()/2, mDstRect.right, mDstRect.top + mDstRect.height()/2, mBeginColor, mEndColor, TileMode.CLAMP);
		
		if(mPaint != null)
			mPaint.setShader(lShader); //LinearGradient, RadialGradient
	}
}
